using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.QuadPack
{
    /// <summary>
    /// The Gauss-Kronrod rule 21.
    /// </summary>
    [Serializable]
    public static class Dqk21Class
    {
        /// <summary>
        /// The Gauss-Kronrod rule 21.
        /// </summary>
        /// <param name="f">The function.</param>
        /// <param name="a">The lower limit of integration.</param>
        /// <param name="b">The upper limit of integration.</param>
        /// <param name="abserr">Estimate of the modulus of the absolute error, which should not exceed abs(i-result).</param>
        /// <param name="resabs">Approximation to the integral j.</param>
        /// <param name="resasc">Approximation to the integal of abs(f-i/(b-a)) over (a,b).</param>
        /// <returns>Approximation to the integral.</returns>
        public static double G_K21(HardRealFunction f, double a, double b, ref double abserr, ref double resabs,
                                   ref double resasc)
        {
            double[] XGK21 = {
                                 0.99565716302580808074, 0.97390652851717172008, 0.93015749135570822600,
                                 0.86506336668898451073, 0.78081772658641689706, 0.67940956829902440623,
                                 0.56275713466860468334, 0.43339539412924719080, 0.29439286270146019813,
                                 0.14887433898163121088, 0.00000000000000000000
                             };
            double[] WGK21 = {
                                 0.01169463886737187428, 0.03255816230796472748, 0.05475589657435199603,
                                 0.07503967481091995277, 0.09312545458369760554, 0.10938715880229764190,
                                 0.12349197626206585108, 0.13470921731147332593, 0.14277593857706008080,
                                 0.14773910490133849137, 0.14944555400291690566
                             };
            double[] WG10 = {
                                0.06667134430868813759, 0.14945134915058059315, 0.21908636251598204400,
                                0.26926671930999635509, 0.29552422471475287017
                            };

            double[] fv1 = new double[10];
            double[] fv2 = new double[10];
            double absc;
            double centr;
            double dhlgth;
            double fc;
            double fsum;
            double fval1;
            double fval2;
            double hlgth;
            double resg;
            double resk;
            double reskh;
            int j;
            int jtw;
            int jtwm1;

            centr = 0.5 * (a + b);
            hlgth = 0.5 * (b - a);
            dhlgth = Math.Abs(hlgth);

            resg = 0.0;
            fc = f.SolveAt(centr);
            resk = fc * WGK21[10];
            resabs = Math.Abs(resk);
            for (j = 0; j < 5; j++)
            {
                jtw = 2 * j + 1;
                absc = hlgth * XGK21[jtw];
                fval1 = f.SolveAt(centr - absc);
                fval2 = f.SolveAt(centr + absc);
                fv1[jtw] = fval1;
                fv2[jtw] = fval2;
                fsum = fval1 + fval2;
                resg += WG10[j] * fsum;
                resk += WGK21[jtw] * fsum;
                resabs = resabs + WGK21[jtw] * (Math.Abs(fval1) + Math.Abs(fval2));
            }
            for (j = 0; j < 5; j++)
            {
                jtwm1 = j * 2;
                absc = hlgth * XGK21[jtwm1];
                fval1 = f.SolveAt(centr - absc);
                fval2 = f.SolveAt(centr + absc);
                fv1[jtwm1] = fval1;
                fv2[jtwm1] = fval2;
                fsum = fval1 + fval2;
                resk = resk + WGK21[jtwm1] * fsum;
                resabs = (resabs) + WGK21[jtwm1] * (Math.Abs(fval1) + Math.Abs(fval2));
            }
            reskh = resk * 0.5;
            resasc = WGK21[10] * Math.Abs(fc - reskh);
            for (j = 0; j < 10; j++)
            {
                resasc = (resasc) + WGK21[j] * (Math.Abs(fv1[j] - reskh) + Math.Abs(fv2[j] - reskh));
            }
            double result = resk * hlgth;
            resabs = (resabs) * dhlgth;
            resasc = (resasc) * dhlgth;
            abserr = Math.Abs((resk - resg) * hlgth);
            if ((resasc != 0.0) && (abserr != 0.0))
            {
                abserr = (resasc) *
                         (((1.0) < (Math.Pow((200.0 * (abserr) / (resasc)), 1.5)))
                              ? (1.0)
                              : (Math.Pow((200.0 * (abserr) / (resasc)), 1.5)));
            }
            if (resabs > Double.MinValue / (50.0 * Double.Epsilon))
            {
                abserr = (((Double.Epsilon * 50.0 * (resabs)) > ((abserr)))
                              ? (Double.Epsilon * 50.0 * (resabs))
                              : ((abserr)));
            }
            return result;
        }
    }
}